﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.SatinAlma;
using ConfExpert.Utility;
using DevExpress.XtraGrid.Columns;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo._7C
{
    public partial class Dikilebilecek : XForm
    {

        int CKBatchId = -1;
        public Dikilebilecek(int ckBatchId)
        {
            InitializeComponent();

            this.CKBatchId = ckBatchId;
            this.Text += "- " + AppSession.Lookups.CKBatch.FindByCKBatchId(CKBatchId).Notes;
        }

        DataTable dt;
        public Dikilebilecek(int ckBatchId, DataTable _dt)
        {
            InitializeComponent();

            this.CKBatchId = ckBatchId;
            this.Text += "- " + AppSession.Lookups.CKBatch.FindByCKBatchId(CKBatchId).Notes;
            dt = _dt;
        }

        DataTable Art;
        BindingSource bs;

        DataSet dsData = null;
        private void Dikilebilecek_Load(object sender, EventArgs e)
        {

            this.
            btnSec.ItemClick += btnSec_ItemClick;
            btnEnAzKumas.ItemClick += btnEnAzKumas_ItemClick;

            Art = new DataTable("Art");
            DataColumn colP = new DataColumn("ProductId", typeof(int));
            DataColumn colQ = new DataColumn("Qty", typeof(int));
            Art.Columns.AddRange(new DataColumn[] { colP, colQ });

            if (dt != null)
                if (dt.Rows.Count > 0)
                    Art.Merge(dt);

            bs = gvArt.Init(Art);
            gvArt.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gvArt.FormatAsNumber(1, "Qty");
            gvArt.AddSummary("Qty");

            gvArt.EnableRowDelete();

            dsData = Session.DataManager.ExecuteDataSet("GetDikilebilen", CKBatchId, Art.Serialize());

            gvDikilebilecek.Init(dsData.Tables[0]);
            gvDikilebilecek.FormatAsNumber(0, "Adet");
            gvDikilebilecek.AddSummary("Adet");

            gvGeriKalan.Init(dsData.Tables[1]);
            gvGeriKalan.FormatAsNumber(0, "Adet");
            gvGeriKalan.AddSummary("Adet");

            if (dsData.Tables.Count > 2)
            {
                gvModel.Init(dsData.Tables[2]);
                List<string> lst = new List<string>();
                foreach (GridColumn col in gvModel.Columns)
                    if (col.FieldName != "Model")
                        lst.Add(col.FieldName);

                gvModel.FormatAsNumber(0, lst.ToArray());
                gvModel.AddSummary(lst.ToArray());
                gvModel.SetCaptions("Kalan:Kalacak Olan");
            }

            gvModel.SetViewCaption("Modele göre gruplu liste");
            gvDikilebilecek.SetViewCaption("Execution listesi");

            btnHesapla.Click += new EventHandler(btnHesapla_Click);
            gvGeriKalan.BestFitColumns();
            gvDikilebilecek.BestFitColumns();
            gvModel.BestFitColumns();
            gvArt.BestFitColumns();
        }

        void btnEnAzKumas_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AppSession.MainForm.ShowMdiChildForm(typeof(EnAzIlave), CKBatchId);
        }

        void btnSec_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (DikilebilecekModel dm = new DikilebilecekModel(gvDikilebilecek.DataTable()))
                dm.ShowDialog();
        }

        void btnHesapla_Click(object sender, EventArgs e)
        {
            bs.EndEdit();
            dsData = Session.DataManager.ExecuteDataSet("GetDikilebilen", CKBatchId, Art.Serialize());
            gvDikilebilecek.RefreshDataSource(dsData.Tables[0], true);
            gvGeriKalan.RefreshDataSource(dsData.Tables[1], true);
            gvModel.RefreshDataSource(dsData.Tables[2], true);
            gvGeriKalan.BestFitColumns();
            gvDikilebilecek.BestFitColumns();
            gvModel.BestFitColumns();
            gvArt.BestFitColumns();
        }
    }
}